package com.dhcc.bpm.modules.workflow.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.modules.common.entity.Plupload;
import com.dhcc.bpm.modules.common.service.IPluploadService;
import com.dhcc.bpm.modules.workflow.entity.WorkOrder;
import com.dhcc.bpm.modules.workflow.service.IWorkOrderService;
import com.dhcc.bpm.modules.workflow.vo.WorkOrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 控制器
 *
 * @author xiafayu
 * @since 2021-11-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm/workorder")
@Api(value = "", tags = "接口")
public class WorkOrderController extends BladeController {

    private IWorkOrderService workOrderService;
    private IPluploadService pluploadService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入workOrder")
    public R<WorkOrderVO> detail(WorkOrder workOrder) {
        WorkOrder detail = workOrderService.getOne(Condition.getQueryWrapper(workOrder));

        List<WorkOrderVO.FilePath> pathList = new ArrayList<>();
        if (null != detail.getFileList()) {
            String[] fileIdList = detail.getFileList().split(",");
            for (String fileId : fileIdList) {
                if (StringUtils.isNotBlank(fileId)) {
                    Plupload plupload = pluploadService.getById(Long.valueOf(fileId));
                    if (null != plupload) {
                        WorkOrderVO.FilePath filePath = new WorkOrderVO.FilePath();
                        filePath.setFilePath(plupload.getFileFullpath());
                        filePath.setId(fileId);
                        filePath.setFileName(plupload.getFileRealname());
                        pathList.add(filePath);
                    }
                }
            }
        }
        WorkOrderVO WorkOrderVO = new WorkOrderVO();
        BeanUtils.copyProperties(detail, WorkOrderVO);

        WorkOrderVO.setFilePathList(pathList);
        return R.data(WorkOrderVO);
    }

    /**
     * 分页
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入workOrder")
    public R<IPage<WorkOrder>> list(WorkOrder workOrder, Query query) {
        IPage<WorkOrder> pages = workOrderService.page(Condition.getPage(query), Condition.getQueryWrapper(workOrder));
        return R.data(pages);
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入workOrder")
    public R save(@Valid @RequestBody WorkOrder workOrder) {
        return R.status(workOrderService.save(workOrder));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入workOrder")
    public R update(@Valid @RequestBody WorkOrder workOrder) {
        return R.status(workOrderService.updateById(workOrder));
    }

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入workOrder")
    public R submit(@Valid @RequestBody WorkOrder workOrder) {
        return R.status(workOrderService.saveOrUpdate(workOrder));
    }


    /**
     * 删除
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(workOrderService.removeByIds(Func.toLongList(ids)));
    }


}
